


let modelImgUrl = "../../../images/";
var tcity = require("../../../js/citys.js");
var session = require('../../../js/session.js');
var requestMode = require('../../../js/requestsMode.js');
Page({
    data :{
        rightArrow:modelImgUrl+"rightArrow_black.png",
        provinces: [],
        name:'',
        phone:'',
        detailedAddress:'',
        province: "",
        citys: [],
        city: "",
        countys: [],
        county: '',
        value: [0,0,11],
        values: [0, 0, 11],
        condition: false,
        ifHasDefaultAddress:true,
        lastValues:[0, 0, 11],
        lastProvince: '',
        lastCity:'',
        lastCounty:''
    },
    onLoad: function () {
        let that = this;
        tcity.init(that);
        var cityData = that.data.cityData;
        const provinces = [];
        const citys = [];
        const countys = [];
        for(let i=0;i<cityData.length;i++){
            provinces.push(cityData[i].name);
        }
        for (let i = 0 ; i < cityData[0].sub.length; i++) {
            citys.push(cityData[0].sub[i].name)
        }
        for (let i = 0 ; i < cityData[0].sub[0].sub.length; i++) {
            countys.push(cityData[0].sub[0].sub[i].name)
        }
        that.setData({
            'provinces': provinces,
            'citys':citys,
            'countys':countys,
            'province':cityData[0].name,
            'city':cityData[0].sub[0].name,
            'county':cityData[0].sub[0].sub[11].name,
            lastProvince: cityData[0].name,
            lastCity:cityData[0].sub[0].name,
            lastCounty:cityData[0].sub[0].sub[11].name,
            lastProvinces: provinces,
            lastCitys:citys,
            lastCountys:countys,
            code:[cityData[0].code,cityData[0].sub[0].code,cityData[0].sub[0].sub[11].code]
        });
    },
    bindChange: function(e) {
        var val = e.detail.value;
        var t = this.data.values;
        var cityData = this.data.cityData;
        if(val[0] != t[0]){
            const citys = [];
            const countys = [];
            for (let i = 0 ; i < cityData[val[0]].sub.length; i++) {
                citys.push(cityData[val[0]].sub[i].name)
            }
            for (let i = 0 ; i < cityData[val[0]].sub[0].sub.length; i++) {
                countys.push(cityData[val[0]].sub[0].sub[i].name)
            }
            this.setData({
                province: this.data.provinces[val[0]],
                city: cityData[val[0]].sub[0].name,
                citys:citys,
                county: cityData[val[0]].sub[0].sub[0].name,
                countys:countys,
                values: val,
                value:[val[0],0,0]
            });
            return;
        }
        if(val[1] != t[1]){
            const countys = [];
            for (let i = 0 ; i < cityData[val[0]].sub[val[1]].sub.length; i++) {
                countys.push(cityData[val[0]].sub[val[1]].sub[i].name)
            }
            this.setData({
                city: this.data.citys[val[1]],
                county: cityData[val[0]].sub[val[1]].sub[0].name,
                countys:countys,
                values: val,
                value:[val[0],val[1],0]
            });
            return;
        }
        if(val[2] != t[2]){
            this.setData({
                county: this.data.countys[val[2]],
                values: val,
                value:[val[0],val[1],val[2]]
            });
            return;
        }
    },
    cancel:function(){
        let lastValues = this.data.lastValues;
        let This = this;
        this.setData({
            condition:!this.data.condition,
            /* ifHasDefaultAddress:true,*/
            value:lastValues,
            values:lastValues,
            lastValues:lastValues,
            /*----*/
            province: This.data.lastProvince,
            city:This.data.lastCity,
            county:This.data.lastCounty,
            /*----*/
            lastProvinces: This.data.lastProvinces,
            lastCitys:This.data.lastCitys,
            lastCountys:This.data.lastCountys,

            provinces:this.data.lastProvinces,
            citys:This.data.lastCitys,
            countys:This.data.countys
        });
    },
    open:function(){
        this.setData({
            condition:!this.data.condition
        })
    },
    sure:function(){
        let choseValues = this.data.value;
        let cityData = this.data.cityData;
        let provinceCode = cityData[choseValues[0]].code;
        let cityCode = cityData[choseValues[0]]['sub'][choseValues[1]].code;
        let areaCode = cityData[choseValues[0]]['sub'][choseValues[1]]['sub'][choseValues[2]].code;
        let This = this;
        this.setData({
            condition:!this.data.condition,
            ifHasDefaultAddress:true,
            lastValues:choseValues,
            value:choseValues,
            values:choseValues,
            province: This.data.province,
            city:This.data.city,
            county:This.data.county,
            lastProvince: This.data.province,
            lastCity:This.data.city,
            lastCounty:This.data.county,
            lastProvinces: This.data.provinces,
            lastCitys:This.data.citys,
            lastCountys:This.data.countys,
            code:[provinceCode,cityCode,areaCode]
        });
    },
    namechange:function(e){
        let name = e.detail.value;
        this.setData({ name : name})
    },
    phonechange:function(e){
        let phone = e.detail.value;
        this.setData({ phone : phone})
    },
    detailedAddressChange:function(e){
        let detailedAddress = e.detail.value;
        this.setData({ detailedAddress : detailedAddress})
    },
    submit:function(){
        let ifHasDefaultAddress = this.data.ifHasDefaultAddress;
        let addressArr = this.data.code;
        let phone = this.data.phone;
        let detailedAddress = this.data.detailedAddress;
        let name = this.data.name;
        let sKey = session.get('sKey');
        if(!ifHasDefaultAddress || !phone || !detailedAddress || !name){
            wx.showModal({
                title: '提示',
                content: '请完善基本信息'
            })
        }else{
            let data = {
                consignee:name,
                mobile:phone,
                provinceid:addressArr[0],
                cityid:addressArr[1],
                areaid:addressArr[2],
                street:detailedAddress
            };
            let header = {
                'sKey' : sKey
            };
            requestMode.sendRequest('address/create','POST',data,header).then(function(res){
                if(res.data.code == 200 ){
                    wx.showToast({
                        title:'添加成功',
                        icon:'success',
                        duration:800
                    })
                }else{
                    wx.showModal({
                        title:'提示',
                        content: res.data.message
                    });
                }
            },function(res){
                wx.showModal({
                    title:'提示',
                    content: '亲，网络可能有点问题哦！'
                });
            })


        }
    }
});